package fetcher

import (
	"fmt"
	"github.com/axgle/mahonia"
	"io/ioutil"
	"net/http"
)

func Fetcher(url string, code string) ([]byte, error) {
	client := &http.Client{}

	req, err := http.NewRequest("GET", url, nil)
	req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50")

	if err != nil {
		return nil, err
	}
	resp, _ := client.Do(req)
	defer resp.Body.Close()

	// 判断返回状态码
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("Fetcher Error 返回码错误:%d", resp.StatusCode)
	}

	// 编码转换
	dec := mahonia.NewDecoder(code)
	rd := dec.NewReader(resp.Body)

	// 读取页面内容
	return ioutil.ReadAll(rd)
}
